package com.hxl.tech.gateway.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@EnableWebFluxSecurity
@SpringBootApplication
public class GatewayApplication {

    @Value("${spring.profiles.active:default}")
    private String profile;

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @Bean
    public SecurityWebFilterChain configure(ServerHttpSecurity http) {
        http.authorizeExchange()
                .pathMatchers("/echo").permitAll()
                .anyExchange().authenticated()
                .and()
                .csrf().disable()
                .oauth2ResourceServer().jwt();
        return http.build();
    }

    @GetMapping("/echo")
    public String echo() { return "Gateway " + profile; }

}
